/**
 * This file is released under the GNU General Public License.
 * Refer to the COPYING file distributed with this package.
 *
 * Copyright (c) 2008-2009 WURFL-Pro srl
 */
package net.sourceforge.wurfl.core.handlers.matchers;

import java.util.SortedSet;

import net.sourceforge.wurfl.core.handlers.Handler;
import net.sourceforge.wurfl.core.handlers.classifiers.FilteredDevices;
import net.sourceforge.wurfl.core.request.WURFLRequest;

import org.apache.commons.lang.StringUtils;

/**
 * @author WURFL-PRO SRL, Rome, Italy
 * @version $Id: AppleMatcher.java 1117 2009-03-20 10:07:30Z filippo.deluca $
 */
public class AppleMatcher extends AbstractMatcher {

	private static final int APPLE_LD_TOLLERANCE = 5;

	public AppleMatcher(Handler handler) {
		super(handler);
	}

	/**
	 * Apply LD(5).
	 */
	protected String lookForMatchingUserAgent(SortedSet userAgentsSet,
			String userAgent) {
		if (logger.isDebugEnabled()) {
			logger.debug("Applying LD(" + APPLE_LD_TOLLERANCE + ") UA: "
					+ userAgent);
		}

		return net.sourceforge.wurfl.core.utils.StringUtils.ldMatch(userAgentsSet, userAgent,
				APPLE_LD_TOLLERANCE);

	}

	/**
	 * if the UA contains "iPhone" return "apple_iphone_ver1" if the UA contains
	 * "iPod" return "apple_ipod_touch_ver1"
	 */
	protected String applyRecoveryMatch(WURFLRequest request, FilteredDevices filteredDevices) {
		if (StringUtils.contains(request.getUserAgent(), "iPhone")) {
			return "apple_iphone_ver1";
		}
		return "apple_ipod_touch_ver1";

	}
}
